package com.joysuch.wwyt.core.controller;

import com.joysuch.wwyt.core.bean.AddBaseEvaluationDetailBean;
import com.joysuch.wwyt.core.bean.BaseEvaluationContentBean;
import com.joysuch.wwyt.core.bean.BaseEvaluationContentBean.SearchBaseEvaluationContentConfig;
import com.joysuch.wwyt.core.bean.BaseEvaluationDetailBean;
import com.joysuch.wwyt.core.bean.BaseEvaluationIndicatorsBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseEvaluationIndicators;
import com.joysuch.wwyt.core.entity.BaseEvaluationIndicatorsContentDetail;
import com.joysuch.wwyt.core.service.BaseEvaluationIndicatorsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/base/evaluationIndicators")
@Api(tags = "/base/evaluationIndicators 评估指标管理")
public class BaseEvaluationIndicatorsController {

    @Autowired
    private BaseEvaluationIndicatorsService baseEvaluationIndicatorsService;

    @PostMapping("/add")
    @ApiOperation(value = "新增评估指标表")
    public ResultBean add(@RequestBody BaseEvaluationIndicatorsBean bean) {
        BaseEvaluationIndicatorsBean s = baseEvaluationIndicatorsService.add(bean);
        return ResultBean.success(s);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改评估指标表")
    public ResultBean update(@RequestBody BaseEvaluationIndicatorsBean bean) {
        try {
            BaseEvaluationIndicatorsBean s = baseEvaluationIndicatorsService.update(bean);
            return ResultBean.success(s);
        } catch (Exception e) {
            return ResultBean.fail(101, "修改失败");
        }
    }

    @GetMapping("/find")
    @ApiOperation(value = "查看评估指标表")
    public ResultBean find(Long id) {
        try {
            BaseEvaluationIndicatorsBean s = baseEvaluationIndicatorsService.find(id);
            return ResultBean.success(s);
        } catch (Exception e) {
            return ResultBean.success(null);
        }
    }

    @PostMapping("/findList")
    @ApiOperation(value = "查看评估指标列表")
    public ResultBean findList() {
        List<BaseEvaluationIndicators> s = baseEvaluationIndicatorsService.findList();
        return ResultBean.success(s);
    }


    @PostMapping("/addDetail")
    @ApiOperation(value = "新增详情")
    public ResultBean addDetail(@RequestBody AddBaseEvaluationDetailBean bean) {
        List<BaseEvaluationIndicatorsContentDetail> s = baseEvaluationIndicatorsService.addDetail(bean);
        return ResultBean.success(s);
    }

    @PostMapping("/updateDetail")
    @ApiOperation(value = "修改详情")
    public ResultBean updateDetail(@RequestBody AddBaseEvaluationDetailBean bean) {
        try {
            List<BaseEvaluationIndicatorsContentDetail> s = baseEvaluationIndicatorsService.updateDetail(bean);
            return ResultBean.success(s);
        } catch (Exception e) {
            return ResultBean.fail(101, "修改失败");
        }
    }

    @GetMapping("/findDetail")
    @ApiOperation(value = "查看详情")
    public ResultBean findDetail(Long id) {
        try {
            List<BaseEvaluationDetailBean> s = baseEvaluationIndicatorsService.findDetail(id);
            return ResultBean.success(s);
        } catch (Exception e) {
            return ResultBean.success(null);
        }
    }

  /*  @GetMapping("/getDetail")
    @ApiOperation(value = "查看详情")
    public ResultBean getDetail(Long id) {
        try {
            List<BaseEvaluationDetailBean> s = baseEvaluationIndicatorsService.getDetail(id);
            return ResultBean.success(s);
        } catch (Exception e) {
            return ResultBean.success(null);
        }
    }*/

    @GetMapping("/delete")
    @ApiOperation(value = "删除评估指标表")
    public ResultBean update(Long id) {
        try {
            baseEvaluationIndicatorsService.delete(id);
            return ResultBean.success("删除成功");
        } catch (Exception e) {
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("/deleteContent")
    @ApiOperation(value = "删除评估内容表")
    public ResultBean deleteContent(Long id) {
        try {
            baseEvaluationIndicatorsService.deleteContent(id);
            return ResultBean.success("删除成功");
        } catch (Exception e) {
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("/deleteDetail")
    @ApiOperation(value = "删除详情")
    public ResultBean deleteDetail(Long id) {
        try {
            baseEvaluationIndicatorsService.deleteDetail(id);
            return ResultBean.success("删除成功");
        } catch (Exception e) {
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultBean page(Pageable page, SearchBaseEvaluationContentConfig condition) {
        if (condition.getEiId() == null) {
            return ResultBean.success(200, "");
        } else {
            Page<BaseEvaluationContentBean> p = baseEvaluationIndicatorsService.findByPage(page, condition);
            return ResultBean.pageData(p.getContent(), p.getTotalElements());
        }
    }
}
